package com.fingard.dsp.bank.directbank.cib01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

@Component("CIB01Deposit")
public class CIB01Deposit extends CIB01Base implements IDeposit {

    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        int beginNumber = 0;//查询的起始笔数
        int totalCount = 0;
        try {
            while (beginNumber <= totalCount && tmpRespCode.length() == 0) {
                //当前页
                beginNumber++;
                XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
                // 写入通用的头部节点
                writeCommonBatPayHeaderEn(xmlWriter);
                xmlWriter.writeStartElement("SECURITIES_MSGSRQV1");
                xmlWriter.writeStartElement("TIMEQUERYTRNRQ");
                xmlWriter.writeElementString("TRNUID", getQryTransID());
                xmlWriter.writeStartElement("RQBODY#PAGE=\"" + beginNumber + "\"");
                //xmlWriter.writeElementString("TITLE", "批量支付");
                //xmlWriter.writeStartElement("ACCTFROM");
                xmlWriter.writeElementString("ACCTID", p_req7507.actNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                //模拟返回
                //WriteBankLogStep2(xmlWriter.toXmlString());
                //String[] tmpStrRet=new String[2];
                //tmpStrRet[0]="";
                //tmpStrRet[1]=FileHelper.readFileText("D:\\zjbr-开发便捷小工具类\\xmlDealWith\\cgn02Test.txt", "utf-8");
                //WriteBankLogStep3(tmpStrRet[1]);
                String returnXml = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<FOX>"), tmpStrRet[1].length());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(returnXml, tmpSbMsg);
                    if (tmpBkReader != null) {
                        String retCode = tmpBkReader.getFirstText("/FOX/SECURITIES_MSGSRSV1/TIMEQUERYTRNRS/STATUS/CODE");
                        if (beginNumber <= 1) {
                            if (tmpBkReader.countElement("/FOX/SECURITIES_MSGSRSV1/BALNTRADEINQUIRYTRNRS/RSBODY/CONTENT") < 1)//返回0表示无记录
                            {
                                tmpRespCode = "-1";
                                tmpRespInfo = "银行返回记录为空！";
                                break;
                            }
                        }
                        if (returnXml.contains("RSBODY MORE=\"Y\"")) {
                            totalCount++;
                        }
                        if ("0".equals(retCode)) {//返回0表示查询成功，可以继续后续数据处理
                            while (tmpBkReader.read("/FOX/SECURITIES_MSGSRSV1/TIMEQUERYTRNRS/RSBODY/CONTENT")) {
                                XElement readXElement = tmpBkReader.readedXElement;
                                //严格按照接口文档返回内容进行映射
                                RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                                retAtsDetail.actNum = p_req7507.actNum;//银行账号
                                retAtsDetail.actArea = p_req7507.actArea;//账号地区区域码
                                retAtsDetail.depositCode = readXElement.getSingleTextNotNull("TIMEACCTID");//定期账号
                                String termType = readXElement.getSingleTextNotNull("TERM");
                                String term = "9";
                                if (termType.equals("M03")) {
                                    term = "3";
                                } else if (termType.equals("M06")) {
                                    term = "4";
                                } else if (termType.equals("Y01")) {
                                    term = "5";
                                } else if (termType.equals("Y02")) {
                                    term = "6";
                                } else if (termType.equals("Y03")) {
                                    term = "7";
                                } else if (termType.equals("Y05")) {
                                    term = "8";
                                }
                                retAtsDetail.terms = term;
                                retAtsDetail.valueDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("DTSTART")));//起息日期 char(8) 格式YYYYMMDD
                                retAtsDetail.oriPrincipal = readXElement.getSingleTextNotNull("OPENAMT");//原始本金 decimal(15，2)
                                retAtsDetail.noteMoney = readXElement.getSingleTextNotNull("BALAMT");//账户余额 decimal(15，2)
                                retAtsDetail.rate = readXElement.getSingleTextNotNull("INTRST");//利率(7位小数)
                                retAtsDetail.startDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("OPENDATE")));//开户日期 char(8) 格式YYYYMMDD
                                retAtsDetail.endDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("DTEND")));//到期日 char(8) 格式YYYYMMDD
                                retAtsDetail.currencyID = convertCurToATS(readXElement.getSingleTextNotNull("CURRENCY"));
                                String status = readXElement.getSingleTextNotNull("STATUS");//1-有效 2-销户 4-冻结 C-抵押
                                if(status.equals("1")) {
                                    retAtsDetail.depositStates = status;
                                }
                                retAtsDetail.dumpFlag = readXElement.getSingleTextNotNull("CONTFLAG");
                                retAts.writeRetDetail(retAtsDetail);
                            }
                        }
                    }

                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无账户");
        }
        retAts.writeEnd();
        return retAts;
    }
}
